﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyManagement.Models;

namespace PartyManagement.Controllers
{
    public class BookEventController : Controller
    {
        private PartyManagementContext db = new PartyManagementContext();

        //
        // GET: /BookEvent/

        public ActionResult Index()
        {
            List<Event> even = new List<Event>();
            int userId = Convert.ToInt32(Session["userID"]);

            if(userId==0) 
                return RedirectToAction("Login","Account");
            else 
                 even= db.Events.Where(p=>p.userID==userId).ToList();

            return View(db.Events.ToList());
        }

        [HttpPost]
        public RedirectToRouteResult Create(Event even)
        {
             int userId = Convert.ToInt32(Session["userID"]);

            if(userId==0) 
                return RedirectToAction("Login","Account");

           
            if (ModelState.IsValid)
            {
                even.userID = userId;
                even.hasMenu = false;
                db.Events.Add(even);
                db.SaveChanges();

            }
          

            return RedirectToAction("Index");
        }



        public ActionResult Edit(int id = 0)
        {
            Event eve = db.Events.Find(id);
            if (eve == null)
            {
                return HttpNotFound();
            }
            ViewBag.hasMenu = eve.hasMenu;
            return View(eve);
        }

        //
        // POST: /BookEvent/Edit/5

        [HttpPost]
        public ActionResult Edit(Event eve)
        {
            if (ModelState.IsValid)
            {
                db.Entry(eve).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(eve);
        }

        //
        // GET: /BookEvent/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Event eve = db.Events.Find(id);
            if (eve == null)
            {
                return HttpNotFound();
            }
            return View(eve);
        }

        //
        // POST: /BookEvent/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {

            //Menu menu = db.Menus.FirstOrDefault(p=>p.eventID==id);
            //db.Menus.Remove(menu);
            Event eve = db.Events.Find(id);
            db.Events.Remove(eve);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}